﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using ARchGL.Platform.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ARchGL.Platform.Utils
{
    public static class BaseServices
    {
        #region 分页相关

        #region IQueryable

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, int skipCount, int maxResultCount)
        {
            return await ToPaged(query, skipCount, maxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedInputDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedAndFilteredInputDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedAndSortedResultRequestDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedResultRequestDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        #endregion

        #region IEnumerable

        public static async Task<IEnumerable<T>> PageByAsync<T>(this IEnumerable<T> query, int skipCount, int maxResultCount)
        {
            return await ToPaged(query, skipCount, maxResultCount);
        }

        public static async Task<IEnumerable<T>> PageByAsync<T>(this IEnumerable<T> query, PagedInputDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IEnumerable<T>> PageByAsync<T>(this IEnumerable<T> query, PagedAndSortedResultRequestDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        #endregion

        private static async Task<IQueryable<T>> ToPaged<T>(IQueryable<T> query, int skipCount, int maxResultCount)
        {
            query = query.Skip(skipCount * maxResultCount);

            if (maxResultCount > 0)
                query = query.Take(maxResultCount);

            return query;
        }

        private static async Task<IEnumerable<T>> ToPaged<T>(IEnumerable<T> query, int skipCount, int maxResultCount)
        {
            query = query.Skip(skipCount * maxResultCount);

            if (maxResultCount > 0)
                query = query.Take(maxResultCount);

            return query;
        }

        #endregion

        #region JsonConvert

        /// <summary>
        /// 将 List&lt;long&gt; 集合序列化为字符串
        /// </summary>
        /// <param name="list">集合数据</param>
        /// <returns></returns>
        public static string SerializeObject(this int[] list)
        {
            var json = JsonConvert.SerializeObject(list);
            return json == "null" ? "[]" : json;
        }

        /// <summary>
        /// 将 List&lt;long&gt; 集合序列化为字符串
        /// </summary>
        /// <param name="list">集合数据</param>
        /// <returns></returns>
        public static string SerializeObject(this List<long> list)
        {
            var json = JsonConvert.SerializeObject(list);
            return json == "null" ? "[]" : json;
        }

        /// <summary>
        /// 将 List&lt;Guid&gt; 集合序列化为字符串
        /// </summary>
        /// <param name="list">集合数据</param>
        /// <returns></returns>
        public static string SerializeObject(this List<Guid> list)
        {
            var json = JsonConvert.SerializeObject(list);
            return json == "null" ? "[]" : json;
        }

        /// <summary>
        /// 将 List&lt;string&gt; 集合序列化为字符串
        /// </summary>
        /// <param name="list">集合数据</param>
        /// <returns></returns>
        public static string SerializeObject(this List<string> list)
        {
            var json = JsonConvert.SerializeObject(list);
            return json == "null" ? "[]" : json;
        }

        #endregion
    }

    public class Asdfasdf<T> : ApplicationService
    {
        private IEnumerable<T> query;

        public Asdfasdf(IEnumerable<T> _query)
        {
            query = _query;
        }

        //public async IReadOnlyList<T> asdfasdf111()
        //{
        //    //return new PagedResultDto<T>(totalCount,
        //    //ObjectMapper.Map<List<T>>(await query.ToListAsync()));

        //    return ObjectMapper.Map<IReadOnlyList<T>>(query.ToList());
        //}
    }
}
